package com.hsylient.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedConfig {

    @Bean
    public DirectExchange businessExchange() {
        return new DirectExchange("a_business_exchange", false, true);
    }

    @Bean
    public Queue businessQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", "a_dead_letter_exchange");
        map.put("x-dead-letter-routing-key", "a_dead_letter_routing_key");
        return new Queue("a_business_queue", false, false, true, map);
    }


    @Bean
    public Queue deadLetterQueue() {
        return QueueBuilder.durable("a_dead_letter_queue").autoDelete().build();
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("a_dead_letter_exchange", false, true);
    }


    @Bean
    public Binding businessBinding(@Qualifier("businessQueue") Queue queue,
                                   @Qualifier("businessExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("a_routing_key");
    }


    @Bean
    public Binding delayedBinding(@Qualifier("deadLetterQueue") Queue queue,
                                  @Qualifier("deadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("a_dead_letter_routing_key");
    }

}
